//
//  TYHImageFuctionView.m
//  OpenPractice
//
//  Created by tanyuehong on 5/1/19.
//  Copyright © 2019年  lijinghua. All rights reserved.
//

#import "TYHImageFuctionView.h"
#import "UIButton+backgroundColorForState.h"

@interface TYHImageFuctionView ()

@property (nonatomic,strong)NSMutableArray *mySubViewsArr;

@end

@implementation TYHImageFuctionView

- (instancetype)initWithFrame:(CGRect)frame imageArr:(NSArray *)imageArr {
    self = [super initWithFrame:frame];
    if (self) {
        self.imageArr = imageArr.copy;
    }
    return self;
}

- (void)setImageArr:(NSArray *)imageArr {
    for (UIView *subView in self.mySubViewsArr) {
        [subView removeFromSuperview];
    }
    [self.mySubViewsArr removeAllObjects];
    if (imageArr.count>0) {
        [self createSubviewsWith:imageArr];
    }
}

- (void)setTopMargin:(CGFloat)topMargin {
    _topMargin = topMargin;
    for (UIView *mySub in self.mySubViewsArr) {
        mySub.y = topMargin;
    }
}

- (void)createSubviewsWith:(NSArray *)imageArr {
    for (int i = 0; i < imageArr.count; i++) {
        NSDictionary *model = [imageArr objectAtIndex:i];
        NSString *imageStr  = [model objectForKey:@"mainIcon"];
        NSString *text      = [model objectForKey:@"title"];
        
        UIButton *subButton = [self getImageStr:imageStr withText:text withFrame:CGRectMake(i*(self.width/imageArr.count), 0, self.width/imageArr.count, 84)];
        [self addSubview:subButton];
        [self.mySubViewsArr addObject:subButton];
    }
}

- (UIButton *)getImageStr:(NSString *)imageStr withText:(NSString *)text  withFrame:(CGRect)frame {
    UIButton *backButtonView = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButtonView setBackgroundColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backButtonView setFrame:frame];
    backButtonView.layer.masksToBounds = YES;
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    imageView.tag = 100;
    imageView.image = [UIImage imageNamed:imageStr];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    imageView.tag = 200;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.font = [UIFont systemFontOfSize:12];
    [titleLabel setTextColor:[UIColor colorWithHex:0x333333]];
    [titleLabel setText:text];
    [titleLabel sizeToFit];
    titleLabel.width += 2;

    imageView.centerX = backButtonView.width *.5f;
    titleLabel.centerX = backButtonView.width*.5f;
    imageView.top = (backButtonView.height-(imageView.height+titleLabel.height+5))*.5f;
    titleLabel.top = imageView.bottom+5;
    
    titleLabel.x  = ceilf(titleLabel.x);
    titleLabel.y  = ceilf(titleLabel.y);
    
    [backButtonView addSubview:imageView];
    [backButtonView addSubview:titleLabel];
    return backButtonView;
}

- (NSMutableArray *)mySubViewsArr {
    if (!_mySubViewsArr) {
        _mySubViewsArr = [NSMutableArray array];
    }
    return _mySubViewsArr;
}

@end
